//
//  PBOnlineDeputeController.m
//  PBHomeDemo
//
//  Created by Playboy on 2017/12/24.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "PBOnlineDeputeController.h"
#import "PBOnlineInputCell.h"
#import "PBCommitCell.h"

static NSString * const kPBOnlineInputCell = @"PBOnlineInputCell";
static NSString * const kPBCommitCell = @"PBCommitCell";

@interface PBOnlineDeputeController ()<UITableViewDelegate,UITableViewDataSource,PBCommitCellDelegate,PBOnlineInputCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *infoDicM;
@end

@implementation PBOnlineDeputeController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
}
- (void)configureView {
    [self.tableView registerNib:[UINib nibWithNibName:kPBOnlineInputCell bundle:nil] forCellReuseIdentifier:kPBOnlineInputCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBCommitCell bundle:nil] forCellReuseIdentifier:kPBCommitCell];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewTapMethod)];
    [self.tableView addGestureRecognizer:tap];
}
- (void)configureData {
    self.infoDicM = [[NSMutableDictionary alloc] init];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 4) {
        PBCommitCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBCommitCell forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    PBOnlineInputCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBOnlineInputCell forIndexPath:indexPath];
    cell.delegate = self;
    [cell refreshWithIndexPath:indexPath];
    return cell;
}

#pragma mark - PBOnlineInputCellDelegate
- (void)getInfoWithtag:(NSString *)tag text:(NSString *)text{
    [self.infoDicM setValue:text forKey:tag];
    NSLog(@"%@",self.infoDicM);
}

#pragma mark - PBCommitCellDelegate
// 提交
- (void)commitMethod {
    if (self.infoDicM.count < 4) {
        [self showHint:@"请填写完整信息"];
        return;
    }
    NSLog(@"%@",self.infoDicM);
    NSDictionary *param = @{@"realName":[self.infoDicM objectForKey:@"0"],
                            @"mobile":[self.infoDicM objectForKey:@"1"],
                            @"cityId":[self.infoDicM objectForKey:@"2"],
                            @"neighborhoodName":[self.infoDicM objectForKey:@"3"]};
    [TMNetwork requestPostWithParam:param url:@"/house/delegation/apply" callback:^(TMBaseReqModel *model) {
        if (model.code == 200) {
            [self showHint:@"我们已收到您的委托，请耐心等待"];
            [self.navigationController popViewControllerAnimated:YES];
        }else {
            [self showHint:model.message];
        }
    }];
}


- (void)tableViewTapMethod {
    [self.view endEditing:YES];
}


#pragma mark - super
- (void)resetNavigationBarItems {
    [super resetNavigationBarItems];
    self.title = @"在线委托";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
